package com.example.ceshi.ui.dashboard;

import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.example.ceshi.Bean.DataDetail;
import com.example.ceshi.Bean.DetailBean;
import com.example.ceshi.Bean.NowUserData;
import com.example.ceshi.DetailActivity;
import com.example.ceshi.MainActivity;
import com.example.ceshi.R;
import com.example.ceshi.SQLite.MySQLiteOpenHelper;
import com.example.ceshi.adapter.DetailAdapter;
import com.example.ceshi.databinding.FragmentDashboardBinding;

import java.util.ArrayList;
import java.util.List;


//包裹页面
public class DashboardFragment extends Fragment {
    private String TAG2 = "ming";
    private MySQLiteOpenHelper mySQLiteOpenHelper;
    private List<DataDetail> mBeanList;
    private ListView detailList;
    private DetailAdapter mDetailAdapter;


    private FragmentDashboardBinding binding;
    private String TAG = "ning";

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        DashboardViewModel dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

//        final TextView textView = binding.textDashboard;
//        dashboardViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);


        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ViewFlipper flipper = view.findViewById(R.id.flipper);
        flipper.startFlipping();


//        view.findViewById(R.id.btn_dashboard).setOnClickListener(this);
        mySQLiteOpenHelper = new MySQLiteOpenHelper(getContext());
        detailList = view.findViewById(R.id.lv_detail);
//        初始化数据
        initData();
//        适配器关联
        initEvent();


//        联系我们
        view.findViewById(R.id.imageView9).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);

                Uri uri = Uri.parse("tel:" + "1231413525");
                intent.setData(uri);
                startActivity(intent);
            }
        });


    }

    private void initData() {
        mBeanList = NowUserData.dataDetailList;
    }

    private void initEvent() {
        mDetailAdapter = new DetailAdapter(getContext(), mBeanList);
        detailList.setAdapter(mDetailAdapter);
        detailList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DataDetail dataDetail = mBeanList.get(position);
//                Toast.makeText(getContext(),"你点击了"+dataDetail.getNumber()+"索引"+position,Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                //        intent.setComponent(componentName);
                Bundle bundle = new Bundle();
                bundle.putString("number", dataDetail.getNumber());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        detailList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                Dialog dialog = new Dialog(getContext());
                View inflate = getLayoutInflater().inflate(R.layout.delete, null);
                Button btn_delete = inflate.findViewById(R.id.fordelete);
                Button btn_nodelete = inflate.findViewById(R.id.nodelete);

                btn_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        进行数据库删除操作
                        Log.d(TAG2, "进行删除操作");
                        mySQLiteOpenHelper.deleteExpressData(mBeanList.get(position).getNumber(), NowUserData.id);

//                        界面删除
                        mBeanList.remove(position);
                        Log.d(TAG2, "位置：" + position);
                        Log.d(TAG2, NowUserData.dataDetailList.toString());
//                        NowUserData.dataDetailList.remove(position);
                        NowUserData.maxNum--;
                        mDetailAdapter.notifyDataSetChanged();
                        dialog.dismiss();
                    }
                });

                btn_nodelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });

                dialog.setContentView(inflate);
                dialog.show();
                return true;
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

//    @Override
//    public void onClick(View view) {
////        ComponentName componentName = new ComponentName("", DetailActivity.class);
////        Intent intent = new Intent(getActivity(), DetailActivity.class);
////        intent.setComponent(componentName);
////        startActivity(intent);
////        Log.d(TAG,((Button)view).getText().toString());
//    }
}